package com.shundr.route;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.common.dao.RouteDao;
import com.shundr.common.util.DialogUtil;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.route.http.RouteRequest;
import com.shundr.route.model.TruckerRouteInfo;

public class RoutePopupwindow extends PopupWindow{
	private Context mContext;
	private List<TruckerRouteInfo> mList = new ArrayList<TruckerRouteInfo>();
	private RouteAdapter mRouteAdapter;
	private ListView mListView;
	private View view;
	
	private RouteDao mRouteDao;
	
	private OnRouteChoiceListener choiceListener;
	public interface OnRouteChoiceListener{
		public void choiceListener(int position,TruckerRouteInfo info);
		
	}

	public OnRouteChoiceListener getOnRouteChoiceListener() {
		return choiceListener;
	}
	public void setOnRouteChoiceListener(OnRouteChoiceListener onRouteChoiceListener) {
		choiceListener = onRouteChoiceListener;
	}
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case RouteRequest.GET_ROUTE_LIST_SUCCESSED:
					List<TruckerRouteInfo> routeinfoList = (List<TruckerRouteInfo>)msg.obj;
					if(routeinfoList!=null&&routeinfoList.size()>0){
//					mRouteDao.syncData(routeinfoList);
						mList.clear();
						TruckerRouteInfo routeInfo=new TruckerRouteInfo();
						routeInfo.setCargoSrcPlace("所有路线");
						mList.add(0, routeInfo);
						mList.addAll(routeinfoList);
						mRouteAdapter.notifyDataSetChanged();
					}else{
						DialogUtil.noticeAlertDialog(mContext, "添加", "您还没有添加路线,现在去添加吗?", new OnDisplayListener() {
							
							@Override
							public void confirm() {
								dismiss();
								mContext.startActivity(new Intent(mContext, RouteSettingActivity.class));
							}
							
							@Override
							public void cancel() {
								dismiss();
							}
						});
					}
					break;
				case LHttpRequest.REQUEST_FAILED:
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}		
	};
	
	public RoutePopupwindow(Context context) {
		super(context);
		this.mContext = context;	
		init();
	}

	private void init() {
		LayoutInflater layoutInflater = LayoutInflater.from(mContext);
		view = layoutInflater.inflate(R.layout.view_route_popupwindow, null);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		mListView=(ListView)view.findViewById(R.id.lv_route_list);
		mRouteAdapter = new RouteAdapter();
		mListView.setAdapter(mRouteAdapter);
		setContentView(view);
		setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
		// 设置动画效果 [R.style.AnimationFade]

		setAnimationStyle(R.style.mypopwindow_anim_style);

		setFocusable(true);
		setBackgroundDrawable(new BitmapDrawable());
		setOutsideTouchable(true);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					TruckerRouteInfo info =mList.get(position);	
					for (TruckerRouteInfo info2 : mList) {
						info2.setSelected(false);
					}
					info .setSelected(true);
					mRouteAdapter.notifyDataSetChanged();
					choiceListener.choiceListener(position, info);
				} catch (Exception e) {
					e.printStackTrace();
				}	
				dismiss();
			}
		});
		
		mRouteDao=new RouteDao();
//		new RouteRequest(mContext, mHandler).getRouteList();
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				List<TruckerRouteInfo> list = mRouteDao.getRouteList(LContext.USER_ID);
				Message message = new Message();
				message.obj = list;
				message.what = RouteRequest.GET_ROUTE_LIST_SUCCESSED;
				mHandler.sendMessage(message);
			}
		}, 0);
	}
	
	public void showPopupWindow(View mView) {
		showAsDropDown(mView);		
	}
	

	@Override
	public void dismiss() {
		super.dismiss();
	}


	class RouteAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int index) {
			return mList.get(index);
		}

		@Override
		public long getItemId(int id) {
			return id;
		}

		public RouteAdapter() {
			super();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			ViewHold viewHold = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_route_info, null);
				viewHold = new ViewHold();
				viewHold.tvFrom = (TextView) convertView
						.findViewById(R.id.tv_from);
				viewHold.tvTo = (TextView) convertView.findViewById(R.id.tv_to);
				viewHold.ivImage=(ImageView)convertView.findViewById(R.id.iv_from_to);
				convertView.setTag(viewHold);
			} else {
				viewHold = (ViewHold) convertView.getTag();
			}
			try {
				TruckerRouteInfo info = mList.get(position);
				if(position==0){
					viewHold.tvFrom.setText(info.getCargoSrcPlace());
					viewHold.tvTo.setText("");
					viewHold.ivImage.setVisibility(View.GONE);
				}else{				
					viewHold.tvFrom.setText(info.getCargoSrcPlace());
					viewHold.tvTo.setText(info.getCargoDstPlace());
					viewHold.ivImage.setVisibility(View.VISIBLE);
				}
				if(info.isSelected()){
					convertView.setBackgroundColor(Color.parseColor("#f0f0f0"));
				}else{
					convertView.setBackgroundResource(R.drawable.item_common_selector);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHold {
			TextView tvFrom;
			TextView tvTo;
			ImageView ivImage;
		}
	}

}
